<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表过渡动画</title>
    <script src="../js/vue.js"></script>
    <style>
        .fade-enter-active,
        .fade-leave-active {
            transition: opacity .5s;
        }

        .fade-enter,
        .fade-leave-to {
            opacity: 0;
        }
    </style>
</head>

<body>
    <div id="app">
        <label for="">序号</label>
        <input type="text" v-model="tmpId" placeholder="请输入序号">
        <p>序号 : {{ tmpId }}</p>

        <label for="">姓名</label>
        <input type="text" v-model="tmpName" placeholder="请输入姓名">
        <p>姓名 : {{ tmpName }}</p>

        <button v-on:click="add">添加</button>

        <h1>基础列表渲染</h1>
        <transition-group name="fade" tag="ul" id="example-1">
            <li v-for="(item, index) in list" :key="item.id" @click="edit(index)">
                序号：{{ item.id }} --- 姓名: {{ item.name }}
                <button @click.stop="deleteItem(index)">删除</button>
            </li>
        </transition-group>
    </div>

    <script type="text/javascript">
        new Vue({
            el: "#app",
            data: {
                tmpId: '',
                tmpName: '',
                list: [
                    { id: 1, name: '张三' },
                    { id: 2, name: '李四' },
                    { id: 3, name: '王五' },
                ]
            },
            methods: {
                add() {
                    if (this.tmpId && this.tmpName) {
                        this.list.push({
                            id: this.tmpId,
                            name: this.tmpName
                        });
                        // 清空输入框
                        this.tmpId = '';
                        this.tmpName = '';
                    }
                },
                deleteItem(index) {
                    this.list.splice(index, 1);
                },
                edit(index) {
                    const newName = prompt('请输入新的姓名', this.list[index].name);
                    if (newName) {
                        this.list[index].name = newName;
                    }
                }
            }
        });
    </script>
</body>

</html>